﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsForms;

using System.Data.SqlClient;

namespace WindowsFormsApp1
{
    public partial class cahx : Form
    {
        DBHelper db = new DBHelper();
        DataSet ds = new DataSet();
        public cahx()
        {
            InitializeComponent();
        }

        private void cahx_Load(object sender, EventArgs e)
        {
          
            db.OpenConnection();

          
            string str = "select count,t1.teamName as hostteamName,t2.teamName as awayteamName,[competitionTime],[competitionPosition],[score],[entryPerson] " +
                "from ScheduleInfo inner join TeamInfo t1 on ScheduleInfo.hostTeamId = t1.id " +
                "inner join TeamInfo t2 on ScheduleInfo.awayTeamId = t2.id";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(str,db.Connection);

            dataAdapter.Fill(ds, "ScheduleInfo");
            
            this.dataGridView1.DataSource = ds.Tables["ScheduleInfo"];

            db.CloseConnection();

        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (this.textBox1.Text.Trim().Equals(string.Empty)) {
                MessageBox.Show("请输入搜索内容！");
                return;
            }

            db.OpenConnection();
            string str2 = "select count,t1.teamName as hostteamName,t2.teamName as awayteamName,[competitionTime],[competitionPosition],[score],[entryPerson] " +
                "from ScheduleInfo inner join TeamInfo t1 on ScheduleInfo.hostTeamId = t1.id " +
                "inner join TeamInfo t2 on ScheduleInfo.awayTeamId = t2.id where t1.teamName like N'%" + this.textBox1.Text + "%'";
            SqlCommand comm = new SqlCommand(str2, db.Connection);
            SqlDataReader read = comm.ExecuteReader();

            if (!read.HasRows)
            {

                MessageBox.Show("目前还没有改赛事！", "错误提示框", MessageBoxButtons.OK, MessageBoxIcon.Error);
                read.Close();
                db.CloseConnection();
                return;
            }
            read.Close();
            db.CloseConnection();

            ds.Tables["ScheduleInfo"].Clear();
            db.OpenConnection();
            string str = "select count,t1.teamName as hostteamName,t2.teamName as awayteamName,[competitionTime],[competitionPosition],[score],[entryPerson] " +
                "from ScheduleInfo inner join TeamInfo t1 on ScheduleInfo.hostTeamId = t1.id " +
                "inner join TeamInfo t2 on ScheduleInfo.awayTeamId = t2.id where t1.teamName like N'%" + this.textBox1.Text + "%'";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(str,db.Connection);
            dataAdapter.Fill(ds, "ScheduleInfo");
        
            this.dataGridView1.DataSource = ds.Tables["ScheduleInfo"];
            db.CloseConnection();


            

        }
    }
}
